package devcken.net;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class NetworkOrder
{
	public static int order(int value)
	{
		return order(value, ByteOrder.nativeOrder());
	}
	
	public static int order(int value, ByteOrder byteOrder)
	{
		ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / 8);
		
		buffer.putInt(value);
		
		buffer.order(byteOrder);
		
		buffer.flip();
		
		return buffer.getInt();
	}
	
	public static long order(long value)
	{
		return order(value, ByteOrder.nativeOrder());
	}
	
	public static long order(long value, ByteOrder byteOrder)
	{
		ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / 8);
		
		buffer.putLong(value);
		
		buffer.order(byteOrder);
		
		buffer.flip();
		
		return buffer.getLong();
	}
	
	public static short order(short value)
	{
		return order(value, ByteOrder.nativeOrder());
	}
	
	public static short order(short value, ByteOrder byteOrder)
	{
		ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / 8);
		
		buffer.putShort(value);
		
		buffer.order(byteOrder);
		
		buffer.flip();
		
		return buffer.getShort();
	}
	
	public static float order(float value)
	{
		return order(value, ByteOrder.nativeOrder());
	}
	
	public static float order(float value, ByteOrder byteOrder)
	{
		ByteBuffer buffer = ByteBuffer.allocate(Float.SIZE / 8);
		
		buffer.putFloat(value);
		
		buffer.order(byteOrder);
		
		buffer.flip();
		
		return buffer.getFloat();
	}
	
	public static double order(double value)
	{
		return order(value, ByteOrder.nativeOrder());
	}
	
	public static double order(double value, ByteOrder byteOrder)
	{
		ByteBuffer buffer = ByteBuffer.allocate(Double.SIZE / 8);
		
		buffer.putDouble(value);
		
		buffer.order(byteOrder);
		
		buffer.flip();
		
		return buffer.getDouble();
	}
}